/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.davial.mgwt.client.activities.patient.detail;


import it.davial.mgwt.client.Patient;
import it.davial.mgwt.client.activities.menu.MenuView;
import it.davial.mgwt.client.ui.widget.base.HasPatientForm;
import it.davial.mgwt.client.utils.CountryList;
import it.davial.mgwt.client.utils.SharedUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.ui.client.widget.FormListEntry;
import com.googlecode.mgwt.ui.client.widget.MDateBox;
import com.googlecode.mgwt.ui.client.widget.MEmailTextBox;
import com.googlecode.mgwt.ui.client.widget.MListBox;
import com.googlecode.mgwt.ui.client.widget.MNumberTextBox;
import com.googlecode.mgwt.ui.client.widget.MTextBox;
import com.googlecode.mgwt.ui.client.widget.WidgetList;

/**
 * Sample implementation of {@link MenuView}.
 */
public class PatientDetailFormViewImpl extends Composite implements HasPatientForm{
	
	private static final Logger logger = Logger.getLogger("DetailCustomerFormViewImpl");
	private Map<String,Widget> customFields = new HashMap<String, Widget>();

	interface Binder extends UiBinder<Widget, PatientDetailFormViewImpl> {
	}
	
	private static final Binder binder = GWT.create(Binder.class);

	@UiField WidgetList lCommon;
	@UiField FlowPanel container;
	private MListBox mList;
	private MTextBox tName;
	private MTextBox tSurname;
	private MTextBox tDate;
	private MTextBox tAddressStreet;
	private MNumberTextBox tAddressCode;
	private MTextBox tAddressCity;
	
	private MEmailTextBox tEmail;
	private MNumberTextBox tPhone;
	private Patient patient;
	
	
	public PatientDetailFormViewImpl() {
		initializeComponents();
		initWidget(binder.createAndBindUi(this));
		populateCommonList();
		
	}

	public ListBox getListBox(){
		return mList;
	}
	
	public MTextBox getName(){
		return tName;
	}

	public MTextBox getSurname(){
		return tSurname;
	}

	public MTextBox getAddress(){
		return tAddressStreet;
	}

	public MTextBox getCity(){
		return tAddressCity;
	}

	public MEmailTextBox getEmail(){
		return tEmail;
	}
	
	public MNumberTextBox getPhone(){
		return tPhone;
	}

	public MNumberTextBox getZip(){
		return tAddressCode;
	}

	@Override
	public Patient createPatient(){
	
		Patient bean = new Patient();
		bean.setName(tName.getValue());
		bean.setSurname(tSurname.getValue());
		bean.setAddressCity(tAddressCity.getValue());
		bean.setAddressStreet(tAddressStreet.getValue());
		bean.setAddressCode(tAddressCode.getValue());
		bean.setDataBirth("01/01/2011");
	//	patient.setAddressProvince(addressProvince)
		bean.setCellular(tPhone.getValue());
		if ( patient != null){
			bean.setIdPatient(patient.getIdPatient());
			bean.setDataInsert(patient.getDataInsert());
		}
		return bean;
	}
	
	public void fillForm ( Patient patient ){
		logger.info("fillForm start, user=" + patient);
		this.patient = patient;
		
		if ( patient == null ) {
			resetForm();
			return;
		}
		tName.setValue(patient.getName());
		tSurname.setValue(patient.getSurname());
	//	tEmail.setValue(user.getMail());
		tPhone.setValue(patient.getCellular());
		tAddressCity.setValue(patient.getAddressCity());
		tAddressCode.setValue(patient.getAddressCode());
	
		}
	
	private void resetForm(){
		tName.setValue(null);
		tSurname.setValue(null);
		tEmail.setValue(null);
		tPhone.setValue(null);
		tAddressCity.setValue(null);
		tAddressCode.setValue(null);
		tDate.setValue(SharedUtils.getFormattedString(new Date(), "dd/MM/yyyy"));
	}
	
	private void initializeComponents(){
		mList = new MListBox();
		tName = new MTextBox();
		tDate = new MTextBox();
		//tDate.setFormat("dd/MM/yyyy");
		tSurname = new MTextBox();
		tEmail = new MEmailTextBox();
		tPhone = new MNumberTextBox();
		tAddressStreet = new MTextBox();
		tAddressCity = new MTextBox();
		tAddressCode = new MNumberTextBox();

		for ( String s : CountryList.getAllCountries() )
			mList.addItem(s);

	}
	
	private void populateCommonList(){
	//	commonTitle.addStyleName(MGWTStyle.getTheme().getMGWTClientBundle().getListCss().listHeader());
		lCommon.add(new FormListEntry("Nome", tName));
		lCommon.add(new FormListEntry("Cognome", tSurname));
		lCommon.add(new FormListEntry("Data di nascita", tDate));
		lCommon.add(new FormListEntry("Email", tEmail));
		lCommon.add(new FormListEntry("Cellulare", tPhone));
		lCommon.add(new FormListEntry("Indirizzo", tAddressStreet));
	//	lCommon.add(new FormListEntry("Country", mList));
		lCommon.add(new FormListEntry("Citta", tAddressCity));
		lCommon.add(new FormListEntry("Zip", tAddressCode));
	}
	

	public void setPatient( Patient patient ){
		logger.info("getting attributes from patient=" + patient.getName());
		this.patient = patient;
		
		
	}

	

}
